//
//  ResignViewController.m
//  悦享
//
//  Created by 韩双 on 16/3/28.
//  Copyright © 2016年 张洪国. All rights reserved.
//

#import "ResignViewController.h"

@interface ResignViewController ()

@property (nonatomic, retain) UITextField *phoneNumber;
@property (nonatomic, retain) UITextField *password;

@property (nonatomic, retain) NSMutableArray *phoneArr;   /**< 查询到的账号(phone)数组 */
@property (nonatomic, retain) UIAlertController *alertVC;    /**< 弹出的alertVC */

@end

@implementation ResignViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createSubviews];
    [self createAlertVC];
    [self updateBmob];
}

#pragma mark - 页面初始化
- (void)createSubviews {
    
    UILabel *lable = [[UILabel alloc] init];
    lable.text = @"+86";
    [self.view addSubview:lable];
    [lable mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.view).with.offset(50);
        make.left.equalTo(self.view).with.offset(20);
        make.size.mas_equalTo(CGSizeMake(50, 30));
        
    }];
    
    self.phoneNumber = [[UITextField alloc] init];
    
    [self.view addSubview:_phoneNumber];
    [_phoneNumber mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(lable).with.offset(60);
        make.centerY.mas_equalTo(lable);
        make.right.equalTo(self.view).with.offset(-20);
        
    }];
    _phoneNumber.clearButtonMode = UITextFieldViewModeWhileEditing;
    _phoneNumber.placeholder = @"请输入手机号";
    _phoneNumber.text = _number;
    _phoneNumber.keyboardType = UIKeyboardTypeNumberPad;  // *****弹出键盘的类型（枚举值）数字键盘
    
    UILabel *password = [[UILabel alloc] init];
    [self.view addSubview:password];
    [password mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(lable);
        make.size.mas_equalTo(lable);
        make.top.equalTo(lable).with.offset(60);
        
    }];
    password.text = @"密码";
    
    self.password = [[UITextField alloc] init];
    [self.view addSubview:_password];
    [_password mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.mas_equalTo(_phoneNumber);
        make.centerY.mas_equalTo(password);
        make.size.equalTo(_phoneNumber);
        
    }];
    _password.secureTextEntry = YES;
    _password.clearButtonMode = UITextFieldViewModeUnlessEditing;
    _password.placeholder = @"请输入密码";
    
    
    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:nextButton];
    
    [nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view).with.offset(20);
        make.right.equalTo(self.view).with.offset(-20);
        make.top.equalTo(password).with.offset(50);
        make.height.mas_equalTo(40);
        
    }];
    [nextButton setTitle:@"注册" forState:UIControlStateNormal];
    [nextButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    nextButton.backgroundColor = [UIColor colorWithRed:0.9859 green:0.3521 blue:0.4104 alpha:1.0];
    nextButton.layer.cornerRadius = 8;
    [nextButton addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark - 创建alertVC 
- (void)createAlertVC {
    
    self.alertVC = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:nil];
    
    [_alertVC addAction:action];
    
}

#pragma mark - 查询bmob
- (void)updateBmob {
    
    self.phoneArr = [NSMutableArray array];
    
    BmobQuery *user_info = [BmobQuery queryWithClassName:@"user_info"];
    //查找user_info表的数据
    [user_info findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        for (BmobObject *obj in array) {
            
            NSString *phone = [obj objectForKey:@"phone"];
            
            [self.phoneArr addObject:phone];
        }
        NSLog(@"查询结果%@", self.phoneArr);
    }];
    
}

- (void)nextAction:(UIButton *)next {
    
    if (_phoneNumber.text.length != 11) {
        
        _alertVC.title = @"手机号码不正确";

        [self presentViewController:_alertVC animated:YES completion:nil];
        
    }else if ([_phoneArr containsObject:_phoneNumber.text]) {
        
        _alertVC.title = @"号码已被注册过了，换一个吧";
        
        [self presentViewController:_alertVC animated:YES completion:nil];
    }else{
        
        if (_password.text.length == 0) {
            
            _alertVC.title = @"请输入密码";
            
            [self presentViewController:_alertVC animated:YES completion:nil];
            
        }else{
            
            /**
             *  账号密码写入bmob
             */
            [self write];
            
            // 还应该判断特殊字符不能成为密码
            [self.navigationController popViewControllerAnimated:YES];
            
        }
        
    }

}

#pragma mark - 注册成功账号写入bmob
- (void)write {
    
    BmobObject  *user_info = [BmobObject objectWithClassName:@"user_info"];

    NSDictionary *dic = @{@"phone" : _phoneNumber.text, @"password" : _password.text};
    [user_info saveAllWithDictionary:dic];
    //异步保存
    [user_info saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
        if (isSuccessful) {
            //创建成功后的动作
            NSLog(@"%@", user_info);
        } else if (error){
            //发生错误后的动作
            NSLog(@"%@",error);
        } else {
            NSLog(@"Unknow error");
        }
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
